#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#else
#include <api/libpub/lib_frontend.h>
#endif
#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"
#include "win_signal.h"

#ifdef SUPPORT_CAS_A
#include "win_com_menu_define.h"
#endif

//#define SUPPORT_DISEQC11
//#define SUPPORT_TONEBURST
/*******************************************************************************
*	Objects definition
*******************************************************************************/
#define FREQ_SETTING_SUPPORT 1

CONTAINER g_win_channelscan;

CONTAINER 	chanscan_item1;		/* Scan Mode*/
CONTAINER 	chanscan_item2;		/* Scan Band */
CONTAINER 	chanscan_item3;		/* Ch No.  */
CONTAINER 	chanscan_item4;		/* Frequency */
CONTAINER 	chanscan_item5;		/* BandWidth */
CONTAINER 	chanscan_item6;		/* Netwrok Search */
CONTAINER 	chanscan_item7;		/* Start */

TEXT_FIELD 	chanscan_txt1;	
TEXT_FIELD 	chanscan_txt2;
TEXT_FIELD 	chanscan_txt3;
TEXT_FIELD 	chanscan_txt4;
TEXT_FIELD 	chanscan_txt5;
TEXT_FIELD 	chanscan_txt6;
TEXT_FIELD 	chanscan_txt7;

MULTISEL 	chanscan_sel1;
MULTISEL 	chanscan_sel2;
MULTISEL 	chanscan_sel3;
EDIT_FIELD 	chanscan_edt4;
MULTISEL 	chanscan_sel5;
MULTISEL 	chanscan_sel6;

TEXT_FIELD 	chanscan_line1;	
TEXT_FIELD 	chanscan_line2;
TEXT_FIELD 	chanscan_line3;
TEXT_FIELD 	chanscan_line4;
TEXT_FIELD 	chanscan_line5;
TEXT_FIELD 	chanscan_line6;
TEXT_FIELD 	chanscan_line7;

TEXT_FIELD 	chanscan_split;

static VACTION chanscan_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanscan_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION chanscan_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanscan_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION chanscan_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanscan_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION chanscan_item_edt_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanscan_item_edt_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static void win_chanscan_load_setting();
void win_chanscan_get_setting(UINT32* freq);
static void switch_scan_mode_select(BOOL select);
void init_ch_no_info(Band_param *buf);
void channel_param(Band_param *buf,UINT32 chan_no,UINT32 *StartFreq,UINT32 *EndFreq,UINT32 *ChannelNo,UINT32 *Bandwidth,BOOL *band_type);
void get_Band_Type(Band_param *buf,UINT32* frequency,BOOL *BandType);
void win_chanscan_set_mode_string(UINT8 input);
static void win_chanscan_set_channel_no(T_NODE *t_node);
static void win_chanscan_set_search_param();
static void chanscan_set_frontend_handler(void);
static void win_chanscan_tuner_frontend(BOOL set_frontend2);

#include <hld/snd/snd.h>
void drvAudioGenTonevoice(UINT8 level, UINT8 init)   //050629 modify the Aerial adjustment to 8 level ming yi
{
	deca_tone_voice((struct deca_device *)dev_get_by_type(NULL, HLD_DEV_TYPE_DECA), level, init);
}

void drvAudioStopTonevoice()
{
	deca_stop_tone_voice((struct deca_device *) dev_get_by_type(NULL, HLD_DEV_TYPE_DECA));   //050629 modify the Aerial adjustment to 8 level ming yi
}

#define WIN_SH_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_HL_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_SL_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_GRY_IDX  WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD
#define CON_ENTER_IDX   WSTL_BUTTON_02_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD

#define SPLIT_LINE_SH	WSTL_LINE_MENU_HD

#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#if Maker_GUI_ON
#define	W_L     	Maker_GUI_SM_L
#define	W_T     	Maker_GUI_SM_T
#define	W_W     	Maker_GUI_SM_W
#define	W_H     	Maker_GUI_SM_H
#else
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif
#endif

#if Maker_GUI_ON
#define CON_L		(W_L+20)
#define CON_T		(W_T)
#define CON_W		(W_W - 60)
#define CON_H		35
#define CON_GAP		6
#else
#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#define CON_H		40
#define CON_GAP		12
#endif

#define TXT_L_OF  	10
#define TXT_W  		250
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4

#define SPLIT_LINE_L	CON_L
#ifdef SUPPORT_TWO_TUNER
#define SPLIT_LINE_T  	(CON_T + 7 * (CON_H + CON_GAP)+2)
#else
#define SPLIT_LINE_T  	(CON_T + 6 * (CON_H + CON_GAP) + 2)
#endif
#define SPLIT_LINE_W	CON_W
#define SPLIT_LINE_H    2

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    chanscan_item_keymap,chanscan_item_callback,  \
    conobj, ID,1)
    
#define LDEF_CON_HL(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,hl,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,hl,CON_SL_IDX,CON_GRY_IDX,   \
    chanscan_item_keymap,chanscan_item_callback,  \
    conobj, ID,1)

#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_MSEL(root, varNum, nxtObj, ID, l, t, w, h,style,cur,cnt,ptabl)	\
    DEF_MULTISEL(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, SEL_SH_IDX,SEL_HL_IDX,SEL_SL_IDX,SEL_GRY_IDX,   \
    chanscan_item_sel_keymap,chanscan_item_sel_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 30,0,style,ptabl,cur,cnt)

#define LDEF_EDIT(root, varNum, nxtObj, ID, l, t, w, h,style,cursormode,pat,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, SEL_SH_IDX,SEL_HL_IDX,SEL_SL_IDX,SEL_GRY_IDX,   \
    chanscan_item_edt_keymap,chanscan_item_edt_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 30,0,style,pat,10,cursormode,NULL,sub,str)


#define LDEF_MENU_ITEM(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,l,t,w,h,\
						resID,style,cur,cnt,ptbl) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL/*&varLine*/   ,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cur,cnt,ptbl)\
    LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_MENU_ITEM_LIST(root,varCon,nxtObj,varTxt,varNum,ID,IDu,IDd,\
						l,t,w,h,resID,style,cur,cnt,ptbl) \
    LDEF_CON_HL(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,CON_ENTER_IDX,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL   ,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cur,cnt,ptbl)

#define LDEF_ANT_LINE(root, varTxt, nxtObj, l, t, w, h, str)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
    0, 0, 0, 0, 0, l, t, w, h, SPLIT_LINE_SH,SPLIT_LINE_SH,SPLIT_LINE_SH,SPLIT_LINE_SH,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,0,str)

#define LDEF_MENU_ITEM_EDT(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,\
						l,t,w,h,resID,style,curMode,pat,sub,str) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_EDIT(&varCon,varNum,NULL,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,curMode,pat,sub,str)\
    LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_MENU_ITEM_TXT(root,varCon,nxtObj,varTxt,varLine,ID,IDu,IDd,\
						l,t,w,h,resID) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,NULL,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID) \
    LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    chanscan_keymap,chanscan_callback,  \
    nxtObj, focusID,0)

#define WIN  g_win_channelscan

char frepat[]  = "r000000~999999";
UINT16 fresub[10];
static char* scan_mode_ids[];
UINT16 scan_mode_ids2[];
char* scan_band_ids[];
UINT16  bandwidth_ids[];
UINT8 scan_mode_sel;
static UINT16 yesno_ids[];
static UINT8 tone_voice_init;

#if (FREQ_SETTING_SUPPORT)
	#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
LDEF_MENU_ITEM(WIN,chanscan_item1,&chanscan_item2,chanscan_txt1,chanscan_sel1, chanscan_line1, 1, 7, 2, \
		CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_INSTALLATION_SCAN_MODE, STRING_ID, 0, 2, scan_mode_ids2)
#else
LDEF_MENU_ITEM(WIN,chanscan_item1,&chanscan_item2,chanscan_txt1,chanscan_sel1, chanscan_line1, 1, 6, 2, \
		CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_INSTALLATION_SCAN_MODE, STRING_ID, 0, 2, scan_mode_ids2)
#endif
#else
	#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
	LDEF_MENU_ITEM(WIN,chanscan_item1,&chanscan_item2,chanscan_txt1,chanscan_sel1, chanscan_line1, 1, 6, 2, \
			CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_INSTALLATION_SCAN_MODE, STRING_ID, 0, 2, scan_mode_ids2)
	#else
	LDEF_MENU_ITEM(WIN,chanscan_item1,&chanscan_item2,chanscan_txt1,chanscan_sel1, chanscan_line1, 1, 5, 2, \
			CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_INSTALLATION_SCAN_MODE, STRING_ID, 0, 2, scan_mode_ids2)	
	#endif
#endif

LDEF_MENU_ITEM(WIN,chanscan_item2,&chanscan_item3,chanscan_txt2,chanscan_sel2,chanscan_line2, 2, 1, 3, \
		CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_INSTALLATION_SCAN_BAND, STRING_ANSI, 0, 2, scan_band_ids)

LDEF_MENU_ITEM(WIN,chanscan_item3,&chanscan_item4,chanscan_txt3,chanscan_sel3, chanscan_line3, 3, 2, 4, \
		CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_INSTALLATION_CHANNEL_NO, STRING_PROC, 0, 3, NULL)

LDEF_MENU_ITEM_EDT(WIN,chanscan_item4,&chanscan_item5,chanscan_txt4,chanscan_edt4, chanscan_line4, 4, 3, 5, \
		CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_DISPLAY_FREQUENCY, NORMAL_EDIT_MODE, CURSOR_SPECIAL, frepat,fresub, display_strs[1])

#if (FREQ_SETTING_SUPPORT)
	#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
LDEF_MENU_ITEM(WIN,chanscan_item5,&chanscan_item6,chanscan_txt5,chanscan_sel5, chanscan_line5, 5, 4, 6, \
		CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_INSTALLATION_BANDWIDTH, STRING_ID, 0, 3, bandwidth_ids)
LDEF_MENU_ITEM(WIN,chanscan_item6,&chanscan_item7,chanscan_txt6,chanscan_sel6, chanscan_line6, 6, 5, 7, \
		CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H, RS_INSTALLATION_NIT_SCAN, STRING_ID, 0, 2, yesno_ids)

LDEF_MENU_ITEM_TXT(WIN,chanscan_item7,NULL,chanscan_txt7, chanscan_line7, 7, 6, 1, \
		CON_L, CON_T + (CON_H + CON_GAP)*6, CON_W, CON_H, RS_COMMON_SEARCH)
#else
	LDEF_MENU_ITEM(WIN,chanscan_item5,&chanscan_item6,chanscan_txt5,chanscan_sel5, chanscan_line5, 5, 4, 6, \
			CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_INSTALLATION_BANDWIDTH, STRING_ID, 0, 3, bandwidth_ids)
	LDEF_MENU_ITEM_TXT(WIN,chanscan_item6,NULL,chanscan_txt6, chanscan_line6, 6, 5, 1, \
			CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H, RS_COMMON_SEARCH)
	#endif
#else
	#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
LDEF_MENU_ITEM(WIN,chanscan_item5,&chanscan_item6,chanscan_txt5,chanscan_sel5, chanscan_line5, 5, 4, 6, \
		CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_INSTALLATION_NIT_SCAN, STRING_ID, 0, 2, yesno_ids)


LDEF_MENU_ITEM_TXT(WIN,chanscan_item6,NULL,chanscan_txt6, chanscan_line6, 6, 5, 1, \
		CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H, RS_COMMON_SEARCH)
	#else
	LDEF_MENU_ITEM_TXT(WIN,chanscan_item5,NULL,chanscan_txt5, chanscan_line5, 5, 4, 1, \
			CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_COMMON_SEARCH)
	#endif
#endif

LDEF_ANT_LINE(WIN, chanscan_split, &chanscan_item1, SPLIT_LINE_L, SPLIT_LINE_T, SPLIT_LINE_W, SPLIT_LINE_H, NULL)

LDEF_WIN(WIN,&chanscan_split,W_L, W_T, W_W, W_H,1)

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/
static char* scan_mode_ids[] = 
{
	"By Channel",
	"By Frequency",
};

UINT16 scan_mode_ids2[] = /* Should have the same order as stream_iso_639lang_abbr*/
{
	RS_SCAN_MODE_CHL,
	RS_SCAN_MODE_FRE,
};

char* scan_band_ids[]=
{
	"VHF",
	"UHF",
};

UINT16 bandwidth_ids[] = 
{
#ifdef DVBT2_1_7M_5M_SUPPORT
    RS_1_7MHZ,
    RS_5MHZ,
#endif
	RS_6MHZ,
	RS_7MHZ,
	RS_8MHZ,
};

static UINT16 yesno_ids[] = 
{
	RS_COMMON_NO,
	RS_COMMON_YES,
};


POBJECT_HEAD chanscan_items[] =
{
	(POBJECT_HEAD)&chanscan_item1,
	(POBJECT_HEAD)&chanscan_item2,
	(POBJECT_HEAD)&chanscan_item3,
	(POBJECT_HEAD)&chanscan_item4,
	(POBJECT_HEAD)&chanscan_item5,
#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON || FREQ_SETTING_SUPPORT)
	(POBJECT_HEAD)&chanscan_item6,	
#endif
#if (FREQ_SETTING_SUPPORT)
	(POBJECT_HEAD)&chanscan_item7,
#endif
    
};

extern P_NODE playing_pnode;
extern UINT8	antset_cur_tuner;
static ID set_frontend = OSAL_INVALID_ID;
static BOOL current_frontend_is_isdbt;


#define SCAN_MODE_CNT		(chanscan_sel1.nCount)
#define SCAN_MODE_IDX		(chanscan_sel1.nSel)
#define SCAN_BAND_CNT		(chanscan_sel2.nCount)
#define SCAN_BAND_IDX		(chanscan_sel2.nSel)
#if (FREQ_SETTING_SUPPORT)
#define BANDWIDTH_CNT		(chanscan_sel5.nCount)
#define BANDWIDTH_IDX		(chanscan_sel5.nSel)
#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
#define NETWORK_SRH_IDX		(chanscan_sel6.nSel)
#endif
#else
#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
#define NETWORK_SRH_IDX		(chanscan_sel5.nSel)
#endif
#endif
typedef enum
{
	SCAN_MODE_ID = 1,
	SCAN_BAND_ID,
	CH_NO_ID,
	FREQUENCY_ID,
#if (FREQ_SETTING_SUPPORT)
	BANDWIDTH_ID,
#endif
#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
    NETWORK_SRH_ID,
#endif
	START_ID
}WIN_ANTENNA_ITEM_ID;

win_scanch_channel_info_t scan_chan_info;
Band_param	country_param[MAX_BAND_COUNT];

#if defined(DVBT2_SUPPORT)
static UINT32 start;
#endif

UINT32 ChNo;
//modify for channel scan mode show the title auto scan on T tuner 2011 11 25
extern BOOL is_auto_scan;

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

#define VACT_POP_UP	(VACT_PASS + 1)

void get_ch_no(UINT32 ChNo, UINT16* unistr)
{
	UINT32 Bandwidth = 0, StartFreq = 0, EndFreq = 0, ChannelNo = 0;
	BOOL bandtype = 0;
	UINT32 freq = 0;
	char str[30];
	
	channel_param(( Band_param*)&country_param, ChNo, &StartFreq, &EndFreq, &ChannelNo, &Bandwidth, &bandtype);
	freq = StartFreq+(Bandwidth/2);
	sprintf(str,"CH%02d (%6d KHz)", ChNo, freq);
	ComAscStr2Uni(str, unistr);
}

void get_frequency(UINT32 ChNo, UINT16* unistr)
{
	UINT32 Bandwidth = 0, StartFreq = 0, EndFreq = 0, ChannelNo = 0;
	BOOL bandtype = 0;
	UINT32 freq = 0;
	char str[30];
	
	channel_param(( Band_param*)&country_param, ChNo, &StartFreq, &EndFreq, &ChannelNo, &Bandwidth, &bandtype);
	freq = StartFreq+(Bandwidth/2);
	sprintf(str,"%6d KHz", freq);
	ComAscStr2Uni(str, unistr);
}

static VACTION chanscan_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_LEFT:
		act = VACT_DECREASE;
		break;
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;
	case V_KEY_ENTER:
		act = VACT_POP_UP;
		break;
	default:
		act = VACT_PASS;
		break;
	}

	return act;	
}

static PRESULT chanscan_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	UINT32 sel;
	UINT8 bID;
	char str[30];
	UINT16 * Uni_str;
	UINT8* pstr;
	SYSTEM_DATA* sys_data;
	POBJECT_HEAD other_antset_menu,item;
	S_NODE s_node;
	UINT16 cur_idx;
	COM_POP_LIST_TYPE_T list_type;
	OSD_RECT rect;
	COM_POP_LIST_PARAM_T param;
	MULTISEL* msel;
	UINT32 Bandwidth=0,StartFreq=0,EndFreq=0,ChannelNo=0;
	BOOL bandtype=0;
	UINT32 Frequency=0;
	POBJECT_HEAD submenu;

	sys_data = sys_data_get();

	bID = OSD_GetObjID(pObj);

	switch(event)
	{
	case EVN_PRE_CHANGE:
		sel = *((UINT32*)param1);
		break;
	case EVN_POST_CHANGE:
		if(bID == SCAN_MODE_ID)
		{
			win_chanscan_load_setting();
			win_chanscan_set_mode_string(SCAN_MODE_IDX);
		}
		else if(bID == SCAN_BAND_ID && SCAN_MODE_IDX == 0)
		{
			if(SCAN_BAND_IDX)
				ChNo = scan_chan_info.UHF_Current_Ch_No;
			else
				ChNo = scan_chan_info.VHF_Current_Ch_No;

			OSD_DrawObject((POBJECT_HEAD )&chanscan_item3,C_UPDATE_ALL);
		}
		//win_signal_refresh();
		api_stop_timer(&set_frontend);
		set_frontend = api_start_timer("SETANT",600,(OSAL_T_TIMER_FUNC_PTR)chanscan_set_frontend_handler);
		break;
	case EVN_REQUEST_STRING:
		sel = param1;
		Uni_str= (UINT16*)param2;
		
		if(bID == CH_NO_ID)
		{
			if(SCAN_BAND_IDX)
				get_ch_no(ChNo, Uni_str);
			else
				get_ch_no(ChNo, Uni_str);
		}
		break;
	case EVN_UNKNOWN_ACTION:
	case EVN_KEY_GOT:
		unact = (VACTION)(param1>>16);
		
		if(unact == VACT_ENTER && bID == START_ID)
		{
			win_chanscan_set_search_param();
			submenu = (POBJECT_HEAD)&g_win_search;
			if(OSD_ObjOpen(submenu, 0xFFFFFFFF) != PROC_LEAVE)
				menu_stack_push(submenu);
			ret = PROC_LOOP;
		}

		item = chanscan_items[bID - 1];

		if(bID == CH_NO_ID)
		{
			if(unact == VACT_ENTER)
			{
			}
			else if(unact == VACT_DECREASE)
			{
				if((ChNo == scan_chan_info.UHF_Start_Ch_No)&&(SCAN_BAND_IDX == 1))
					ChNo = scan_chan_info.UHF_End_Ch_No;
				else if((ChNo == scan_chan_info.VHF_Start_Ch_No)&&(SCAN_BAND_IDX == 0))
					ChNo = scan_chan_info.VHF_End_Ch_No;
				else
				{
				    //ChNo maybe 0, should judge here
				    if(ChNo)
				        ChNo--;
				}				
			}
			else if(unact == VACT_INCREASE)
			{
				if((ChNo == scan_chan_info.UHF_End_Ch_No)&&(SCAN_BAND_IDX == 1))
					ChNo = scan_chan_info.UHF_Start_Ch_No;
				else if((ChNo == scan_chan_info.VHF_End_Ch_No)&&(SCAN_BAND_IDX == 0))
					ChNo = scan_chan_info.VHF_Start_Ch_No;
				else
					ChNo++;
			}
			OSD_TrackObject(item, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
			api_stop_timer(&set_frontend);
			set_frontend = api_start_timer("SETANT",600,(OSAL_T_TIMER_FUNC_PTR)chanscan_set_frontend_handler);
			//win_signal_refresh();
			ret = PROC_LOOP;
		}
		//win_signal_refresh();
		break;
	}

	return ret;	
}

static VACTION chanscan_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;
	switch(key)
	{
	case V_KEY_ENTER:
	//case V_KEY_RIGHT:		
		act = VACT_ENTER;
		break;
	default:
		act = VACT_PASS;
		break;
	}
	return act;
}

static PRESULT chanscan_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	VACTION unact;
	POBJECT_HEAD submenu;
	UINT8 back_saved;
	bID = OSD_GetObjID(pObj);

	switch(event)
	{
	case EVN_UNKNOWN_ACTION:
	case EVN_KEY_GOT:
		unact = (VACTION)(param1>>16);
		if(unact == VACT_ENTER && bID == START_ID)
		{
			win_chanscan_tuner_frontend(TRUE);
			win_chanscan_set_search_param();
			submenu = (POBJECT_HEAD)&g_win_search;
#ifdef ANTENNA_INSTALL_BEEP	
			if(tone_voice_init ==1)
			{
			#if(defined(DVBT_BEE_TONE) && ANTENNA_INSTALL_BEEP == 1)
				api_audio_stop_tonevoice();
			#elif (defined(AUDIO_SPECIAL_EFFECT) && ANTENNA_INSTALL_BEEP == 2)
				api_audio_beep_stop();
			#endif
			tone_voice_init =0;
			}
#endif
			
			if(OSD_ObjOpen(submenu, 0xFFFFFFFF) != PROC_LEAVE)
				menu_stack_push(submenu);
			ret = PROC_LOOP;
		}
		break;
	}

	return ret;
}

static VACTION chanscan_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
		break;
	}

	return act;	
}

#ifdef ANTENNA_INSTALL_BEEP	
extern UINT8 beep_bin_array[];
extern UINT32 beep_bin_array_size;
#endif

static PRESULT chanscan_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT16 default_satidx;
	UINT32 sat_cnt;
	MULTISEL* msel;
    struct nim_device* nim_dev;
    UINT16 title_id;

#if defined(ANTENNA_INSTALL_BEEP) || defined(DVBT2_LITE_SUPPORT)			
	SYSTEM_DATA* sys_data;
	UINT8 level,quality,lock;
	static UINT32 interval;
	sys_data = sys_data_get();
#endif
	

	switch(event)
	{
	case EVN_PRE_OPEN:
        if(param2 != MENU_OPEN_TYPE_STACK)
        {
            if((param2 & MENU_FOR_ISDBT) == MENU_FOR_ISDBT)
                current_frontend_is_isdbt = TRUE;           
            else
                current_frontend_is_isdbt = FALSE;
        }
        if(current_frontend_is_isdbt)
            title_id = RS_ISDBT_CHAN_SCAN;
        else            
            title_id = RS_DVBT_CHAN_SCAN;
        	is_auto_scan=FALSE;//modify for channel scan mode show the title auto scan on T tuner 2011 11 25
		wincom_open_title(pObj, title_id,0);
		switch_scan_mode_select(SCAN_MODE_IDX);
		win_chanscan_load_setting();
		
		OSD_SetContainerFocus((CONTAINER*)pObj,1);
		snd_io_control((struct snd_device *)dev_get_by_id(HLD_DEV_TYPE_SND, 0),SND_DAC_MUTE,0);
		break;
	case EVN_POST_OPEN:
#ifdef ANTENNA_INSTALL_BEEP				
			tone_voice_init = 0;
#endif
        win_chanscan_set_mode_string(SCAN_MODE_IDX);
		//win_signal_open(pObj);
		nim_dev = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 0);
        if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev, HLD_DEV_TYPE_NIM) && 
            ((board_frontend_is_isdbt(0) && current_frontend_is_isdbt) || 
             (!board_frontend_is_isdbt(0) && !current_frontend_is_isdbt)))
		    cur_tuner_idx = 0;
        else
        {
            nim_dev = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 1);
            if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev, HLD_DEV_TYPE_NIM) && 
                ((board_frontend_is_isdbt(1) && current_frontend_is_isdbt) || 
                 (!board_frontend_is_isdbt(1) && !current_frontend_is_isdbt)))
                cur_tuner_idx = 1;
        }
#ifdef SUPPORT_TWO_TUNER
		win_signal_open_ex(pObj,pObj->frame.uLeft,pObj->frame.uTop + 7 * (CON_H + CON_GAP) + 20);
#else
		win_signal_open_ex(pObj,pObj->frame.uLeft,pObj->frame.uTop + 7 * (CON_H + CON_GAP) + 20);
#endif
        
		win_chanscan_tuner_frontend(FALSE);           
		win_signal_refresh();           
		//win_signal_update();
		break;
	case EVN_PRE_CLOSE:
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
#ifdef ANTENNA_INSTALL_BEEP	
		if(sys_data->install_beep)
		{
		#if(defined(DVBT_BEE_TONE) && ANTENNA_INSTALL_BEEP == 1)
			api_audio_stop_tonevoice();
		#elif (defined(AUDIO_SPECIAL_EFFECT) && ANTENNA_INSTALL_BEEP == 2)
			api_audio_beep_stop();
		#endif
		}
#endif
		break;
	case EVN_POST_CLOSE:
		win_signal_close();        
		//api_nim_stop_autoscan(); 
		sys_data_save(1);
		//drvAudioStopTonevoice(); 
		snd_io_control((struct snd_device *)dev_get_by_id(HLD_DEV_TYPE_SND, 0),SND_DAC_MUTE,1);
		break;
	case EVN_MSG_GOT:
		if(param1 == CTRL_MSG_SUBTYPE_STATUS_SIGNAL)
		{
#if defined(DVBT2_SUPPORT)
            lock    = (UINT8)(param2>>0);
			if(lock)
			{
                start = osal_get_tick(); 
			}
            else
            {
            	UINT32 timeout = 4000; //It shall not less then 4s.
                //Auto retry to detect and lock the signal that it's either DVB-T or DVB-T2 signal.
			#ifdef DVBT2_LITE_SUPPORT 
				if(sys_data->t2_lite_support)
            		timeout = 8000;
			#endif			
                UINT32 now;
                now = osal_get_tick();
                if (now - start >= timeout)
                {
                    win_chanscan_tuner_frontend(FALSE);
                    start = osal_get_tick();
                    break;
                }
            }
#endif						
            
			win_signal_refresh();
			//win_signal_update();		
#ifdef ANTENNA_INSTALL_BEEP				
			if(sys_data->install_beep)
			{
				level   = (UINT8)(param2>>16);
				quality = (UINT8)(param2>>8);
				lock    = (UINT8)(param2>>0);
			#if(defined(DVBT_BEE_TONE) && ANTENNA_INSTALL_BEEP == 1)	
				if(quality<20)
			              level = 0;
			            else if(quality<40)
			                level = 1;
			            else if(quality<55)
			                level = 3;
			            else if(quality <70)
			                level = 5;
			            else
			                level = 7;
				api_audio_gen_tonevoice(level, tone_voice_init);				
			#elif (defined(AUDIO_SPECIAL_EFFECT) && ANTENNA_INSTALL_BEEP == 2)
				if(lock)
					level = 100;//100/quality;
				else
					level = 1;//100/quality;				
				if(!tone_voice_init || level!=interval)
				{
					if(!tone_voice_init)
						api_audio_beep_start(beep_bin_array,beep_bin_array_size);
					interval = level;
					api_audio_beep_set_interval(interval);					
				}
			#endif
				tone_voice_init = 1;
			}
#endif						
		}
		else if(param1 == CTRL_MSG_SUBTYPE_CMD_TP_TURN)
		{
			win_chanscan_tuner_frontend(FALSE);
		}  
		break;
	}
	return ret;
}

//add judge here 2011-10-31
static void win_chanscan_scanband_setting()
{
    //add judge here 2011-10-31 for if the VHF/UHF have no channel, can't change Scan Band
    if(scan_chan_info.VHF_Current_Ch_No ==0 && scan_chan_info.UHF_Current_Ch_No !=0)
    {
        
        //if the VHF have no channel, don't let select Scan Band
        OSD_SetAttr(&chanscan_item2, C_ATTR_INACTIVE);
        OSD_SetAttr(&chanscan_txt2, C_ATTR_INACTIVE);
        OSD_SetAttr(&chanscan_sel2, C_ATTR_INACTIVE);
        SCAN_BAND_IDX=1;//set to UHF
    }
    else if(scan_chan_info.UHF_Current_Ch_No ==0 && scan_chan_info.VHF_Current_Ch_No !=0)
    {
        
        //if the UHF have no channel, don't let select Scan Band
        OSD_SetAttr(&chanscan_item2, C_ATTR_INACTIVE);
        OSD_SetAttr(&chanscan_txt2, C_ATTR_INACTIVE);
        OSD_SetAttr(&chanscan_sel2, C_ATTR_INACTIVE);
        SCAN_BAND_IDX=0;//set to VHF
    } 
}

static void win_chanscan_load_setting()
{
	EDIT_FIELD	*edf;
	MULTISEL	*msel;
	T_NODE		t_node;
	P_NODE		p_node;
	UINT32		p_num;
	UINT32		Bandwidth=0,StartFreq=0,EndFreq=0,ChannelNo=0;
	BOOL		bandtype=0;
	UINT16		cur_chan;
	UINT8		str[16];
	SYSTEM_DATA*	sys_data;
	UINT8	uCurCountryIdx;
    UINT16 sat_id;
    sys_data = sys_data_get();

	uCurCountryIdx = sys_data->country;
    if(current_frontend_is_isdbt)
    {
        sat_id = get_frontend_satid(FRONTEND_TYPE_T, 1);
        get_isdbt_bandparam(uCurCountryIdx, (Band_param *)&country_param);
    }
    else
    {
        sat_id = get_frontend_satid(FRONTEND_TYPE_T, 0);
        get_dvbt_bandparam(uCurCountryIdx, (Band_param *)&country_param);
    }
	//get_default_bandparam(uCurCountryIdx, (Band_param *)&country_param);	
	
	MEMSET(&scan_chan_info, 0, sizeof(scan_chan_info));
	init_ch_no_info(( Band_param*)&country_param);

	//p_num = get_node_num(TYPE_PROG_NODE,NULL);
	MEMSET(&p_node, 0, sizeof(P_NODE));
	//p_num = get_prog_num(VIEW_ALL | TV_CHAN | RADIO_CHAN , 0);
	p_num = db_check_prog_exist(VIEW_ALL | PROG_TVRADIO_MODE , 0);//speed UI
	if (0 != p_num)
	{
		cur_chan = sys_data_get_cur_group_cur_mode_channel();
		get_prog_at(cur_chan,&p_node);
	}

    //change on 2011-10-28 shoud check p_num, if p_num is not zero, can use p_node to get t_node
    if(p_node.sat_id == sat_id && p_num)//MAX_SAT_NUM-1)
    {
		get_tp_by_id(p_node.tp_id,&t_node);
		win_chanscan_set_channel_no(&t_node);
		channel_param(( Band_param*)&country_param,ChNo,&StartFreq,&EndFreq,&ChannelNo,&Bandwidth,&bandtype);
		BANDWIDTH_IDX=((Bandwidth/1000)-6);
	}
	else
	{
		ChNo=(country_param[0].band_type&0x007f);
		SCAN_BAND_IDX=((country_param[0].band_type&0x80)!=0?1:0);
		
		if(SCAN_BAND_IDX)
			ChNo = scan_chan_info.UHF_Current_Ch_No;
		else
			ChNo = scan_chan_info.VHF_Current_Ch_No;

		BANDWIDTH_IDX=((country_param[0].bandwidth/1000)-6);
	}
	
	//add judge here 2011-10-31 for avoid the VHF/UHF have no channel
	if(scan_chan_info.VHF_Current_Ch_No ==0 && scan_chan_info.UHF_Current_Ch_No !=0)
	{     
		SCAN_BAND_IDX=1;//set to UHF
		ChNo = scan_chan_info.UHF_Current_Ch_No;
	}
	else if(scan_chan_info.UHF_Current_Ch_No ==0 && scan_chan_info.VHF_Current_Ch_No !=0)
	{
		SCAN_BAND_IDX=0;//set to VHF
		ChNo = scan_chan_info.VHF_Current_Ch_No;
	}

	t_node.band_type = SCAN_BAND_IDX;
	msel = &chanscan_sel2;
	OSD_SetMultiselSel(msel, SCAN_BAND_IDX);

	edf = &chanscan_edt4;
	channel_param(( Band_param*)&country_param, ChNo, &StartFreq, &EndFreq, &ChannelNo, &Bandwidth, &bandtype);
	OSD_SetEditFieldSuffix(edf, STRING_ANSI, (UINT32)" KHz");
	OSD_SetEditFieldContent(edf, STRING_NUMBER, StartFreq+(Bandwidth/2));
	OSD_SetEditFieldStyle(edf,NORMAL_EDIT_MODE | SELECT_STATUS);

	t_node.bandwidth=BANDWIDTH_IDX;
	msel = &chanscan_sel5;
	OSD_SetMultiselSel(msel, BANDWIDTH_IDX);
}

static void chanscan_set_frontend_handler(void)
{
	ap_send_msg(CTRL_MSG_SUBTYPE_CMD_TP_TURN,0,FALSE);
}

#ifdef DVBT2_1_7M_5M_SUPPORT
static UINT32 get_input_bandwidth(void)
{
   UINT32 bandwidth = 0;

   switch(BANDWIDTH_IDX)
   {
    case 0:
        bandwidth = 1700; //1000??
        break;
    case 1:
        bandwidth = 5000;
        break;
    case 2:
        bandwidth = 6000;
        break;
    case 3:
        bandwidth = 7000;
        break;
    case 4:
        bandwidth = 8000;
        break;
    default:
        PRINTF("%s(): not support such bandwidth(%dMHZ)\n", __FUNCTION__, BANDWIDTH_IDX);
   }
    
    return bandwidth;
}
#else
static UINT32 get_input_bandwidth(void)
{
   UINT32 bandwidth = 0;

   switch(BANDWIDTH_IDX)
   {
    case 0:
        bandwidth = 6000;
        break;
    case 1:
        bandwidth = 7000;
        break;
    case 2:
        bandwidth = 8000;
        break;
    default:
        PRINTF("%s(): not support such bandwidth(%dMHZ)\n", __FUNCTION__, BANDWIDTH_IDX);
   }
    
    return bandwidth;
}
#endif
static void win_chanscan_tuner_frontend(BOOL set_frontend2)
{
	struct as_service_param param;
	UINT32 Frequency=0,bandwidth=0,Bandwidth=0,StartFreq=0,EndFreq=0,ChannelNo=0;	
	struct nim_device *nim_dev1 = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 0);
	struct nim_device *nim_dev2 = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, 1);
    union ft_xpond xponder;
    static struct ft_frontend ft;
	struct nim_device* nim_dev;
	
	MEMSET(&param, 0, sizeof(struct as_service_param));
    MEMSET(&ft, 0, sizeof(struct ft_frontend));
    MEMSET(&xponder, 0, sizeof(union ft_xpond));
    
	if(SCAN_MODE_IDX==0)  //by channel
	{
		channel_param(( Band_param*)&country_param,ChNo,&StartFreq,&EndFreq,&ChannelNo,&Bandwidth,(BOOL*)&param.ft[0].t_param.band_type);
		Frequency = StartFreq+Bandwidth/2;
	}
	else  //by frequency
	{
		win_chanscan_get_setting(&Frequency);
		get_Band_Type(( Band_param*)&country_param,&Frequency,(BOOL*) &param.ft[0].t_param.band_type);

        Bandwidth = get_input_bandwidth();

        if(current_frontend_is_isdbt)
		    StartFreq = Frequency - Bandwidth/2;
	}

    if(!current_frontend_is_isdbt)
    {
        //so for, because frequency of nim_m3101 only valid between 40000Khz-
        // 900000Khz, beyond the range nim_3101 do not change the frequency. 
        // but we wan to get real time lock status, so we set a valid frequency
        // (40000+1) to change frequency.
        if (Frequency <= 40000 || Frequency >= 900000)
            Frequency = 40001;  
    }
    
    if(!current_frontend_is_isdbt)
    {
        ft.xpond.t_info.type = FRONTEND_TYPE_T;
        ft.xpond.t_info.frq = Frequency;
        ft.xpond.t_info.band_width = Bandwidth;
#if defined(DVBT2_SUPPORT)
        ft.xpond.t_info.usage_type = (UINT8)USAGE_TYPE_AERIALTUNE;
#else
        ft.xpond.t_info.usage_type = (UINT8)USAGE_TYPE_CHANSCAN;
#endif        

        if(NULL != nim_dev1)
        {
            if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev1, HLD_DEV_TYPE_NIM) && !board_frontend_is_isdbt(0))
            {
//              frontend_tuning(nim_dev1, NULL, &xponder, 1);
				#ifdef DVBT2_LITE_SUPPORT
				SYSTEM_DATA* sys_data;
				sys_data = sys_data_get();
				nim_io_control(nim_dev1, NIM_DRIVER_T2_LITE_ENABLE, (UINT32)(sys_data->t2_lite_support));
				#endif
                ft.nim = nim_dev1;
                UIChChgAerialSignalMonitor(&ft);
            }
        }
        
        UINT32 type1 = dev_get_device_sub_type(nim_dev1, HLD_DEV_TYPE_NIM);
        UINT32 type2 = dev_get_device_sub_type(nim_dev2, HLD_DEV_TYPE_NIM);
        if((set_frontend2&&(NULL != nim_dev2))
         ||((type1==FRONTEND_TYPE_S&& type2==FRONTEND_TYPE_T)||(type1==FRONTEND_TYPE_T&& type2==FRONTEND_TYPE_S)))
        {
            //frontend_set_nim(nim_dev2, NULL, &xponder, 1);
            if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev2, HLD_DEV_TYPE_NIM) && !board_frontend_is_isdbt(1))
            {
//              frontend_tuning(nim_dev2, NULL, &xponder, 1);
				#ifdef DVBT2_LITE_SUPPORT
				SYSTEM_DATA* sys_data;
				sys_data = sys_data_get();
				nim_io_control(nim_dev2, NIM_DRIVER_T2_LITE_ENABLE, (UINT32)(sys_data->t2_lite_support));
				#endif

                ft.nim = nim_dev2;        
                UIChChgAerialSignalMonitor(&ft);
            }
        }
        
    }
    else
    {
        ft.xpond.t_info.type = FRONTEND_TYPE_T;
        ft.xpond.t_info.frq = Frequency;
        ft.xpond.t_info.band_width = Bandwidth; // KHz
        ft.xpond.t_info.usage_type = (UINT8)USAGE_TYPE_CHANSCAN;  
    
        UINT32 type1 = dev_get_device_sub_type(nim_dev1, HLD_DEV_TYPE_NIM);
        UINT32 type2 = dev_get_device_sub_type(nim_dev2, HLD_DEV_TYPE_NIM);


        if(FRONTEND_TYPE_T == type1 && board_frontend_is_isdbt(0))
        {
            ft.nim = nim_dev1;
            UIChChgAerialSignalMonitor(&ft);
        }
        else
        {
            if(FRONTEND_TYPE_T == type2 && board_frontend_is_isdbt(1))
            {
                ft.nim = nim_dev2;        
                UIChChgAerialSignalMonitor(&ft);
            }
        }
        
/*
        if(set_frontend2&&(NULL != nim_dev2))
        {
            if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev2, HLD_DEV_TYPE_NIM) && board_frontend_is_isdbt(1))
            {
                ft.nim = nim_dev2;        
                UIChChgAerialSignalMonitor(&ft);
            }
        }
*/        
    }
    
#if defined(DVBT2_SUPPORT)
    start = osal_get_tick();
#endif

}

static void win_chanscan_set_search_param()
{
	UINT32  search_mode;
	UINT32 prog_type;
	struct as_service_param param;
	UINT32 addr, len;
	UINT32 Frequency=0,bandwidth=0,Bandwidth=0,StartFreq=0,EndFreq=0,ChannelNo=0;	
	T_NODE t_node;
	INT32 ret;
	T_NODE temp_t_node;
    UINT16 sat_id;
    
	MEMSET(&param, 0, sizeof(struct as_service_param));

	MEMSET(&t_node, 0, sizeof(T_NODE));

    if(current_frontend_is_isdbt)
        sat_id = get_frontend_satid(FRONTEND_TYPE_T, 1);
    else
        sat_id = get_frontend_satid(FRONTEND_TYPE_T, 0);

	t_node.sat_id = sat_id;// MAX_SAT_NUM-1;
	t_node.ft_type = FRONTEND_TYPE_T;

	if(SCAN_MODE_IDX==0)  //by channel
	{
		channel_param(( Band_param*)&country_param,ChNo,&StartFreq,&EndFreq,&ChannelNo,&Bandwidth,(BOOL*)&param.ft[0].t_param.band_type);
		t_node.frq = StartFreq+Bandwidth/2;
		t_node.bandwidth = Bandwidth/1000;
		t_node.band_type = SCAN_BAND_IDX;
		
		param.ft[0].t_param.band_type=SCAN_BAND_IDX;
	}
	else  //by frequency
	{
		win_chanscan_get_setting(&Frequency);
		get_Band_Type(( Band_param*)&country_param,&Frequency,(BOOL*) &param.ft[0].t_param.band_type);

		bandwidth = get_input_bandwidth();

		t_node.frq = Frequency;
		t_node.bandwidth = bandwidth/1000;	
		t_node.band_type = param.ft[0].t_param.band_type;
	}

	recreate_tp_view(VIEW_SINGLE_SAT, 1);
	ret = lookup_node(TYPE_TP_NODE, &t_node, t_node.sat_id);
	if (ret != SUCCESS)
	{
		add_node(TYPE_TP_NODE, t_node.sat_id, &t_node);
		update_data();
		libc_printf("add a new node\n");
	}
	
#if defined(_MHEG5_V20_ENABLE_)
	param.as_prog_attr = P_SEARCH_ALL|P_SEARCH_DATA;
#else
	param.as_prog_attr = P_SEARCH_ALL;
#endif

#if (AOV_OSD_NETWORK_SEARCH_ENABLE == SYS_FUNC_ON)
    if(NETWORK_SRH_IDX == 1)
        param.as_prog_attr |= P_SEARCH_NIT;    
#endif

	param.as_p_add_cfg = PROG_ADD_REPLACE_OLD;
    
	param.as_method = AS_METHOD_FREQ_BW;

	param.as_frontend_type = FRONTEND_TYPE_T;
	param.as_sat_cnt = 1;
	param.sat_ids[0] = sat_id;// MAX_SAT_NUM-1;//1;

	//param.as_sat_id = 1;
	
	param.as_handler = NULL;

	param.as_from = t_node.tp_id;
	param.as_to = 0;

	//param.ft[0].t_param.bandwidth = bandwidth;
   // param.ft_count = 1;

	// set param 
	win_search_set_param(&param);

	update_data();
	
	addr = __MM_AUTOSCAN_DB_BUFFER_ADDR;
	addr &= 0x8fffffff;
	len = __MM_AUTOSCAN_DB_BUFFER_LEN;

	db_search_init((UINT8 *)addr, len);
}


/* switch Scan Mode by Channel or by Frequency */
static void switch_scan_mode_select(BOOL select)
{   
	if(select) /* By Frequency */
	{
		if( !OSD_CheckAttr(&chanscan_item2, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&chanscan_item2, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_txt2, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_sel2, C_ATTR_INACTIVE);
		}
		if( !OSD_CheckAttr(&chanscan_item3, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&chanscan_item3, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_txt3, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_sel3, C_ATTR_INACTIVE);
		}
		if( !OSD_CheckAttr(&chanscan_item4, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&chanscan_item4, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_txt4, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_edt4, C_ATTR_ACTIVE);
		}
#if (FREQ_SETTING_SUPPORT)
		if( !OSD_CheckAttr(&chanscan_item5, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&chanscan_item5, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_txt5, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_sel5, C_ATTR_ACTIVE);
		}
#endif
	}
	else  /* By Channel */
	{
		if( !OSD_CheckAttr(&chanscan_item2, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&chanscan_item2, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_txt2, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_sel2, C_ATTR_ACTIVE);
		}
		if( !OSD_CheckAttr(&chanscan_item3, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&chanscan_item3, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_txt3, C_ATTR_ACTIVE);
			OSD_SetAttr(&chanscan_sel3, C_ATTR_ACTIVE);
		}
		if( !OSD_CheckAttr(&chanscan_item4, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&chanscan_item4, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_txt4, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_edt4, C_ATTR_INACTIVE);
		}
#if (FREQ_SETTING_SUPPORT)
		if( !OSD_CheckAttr(&chanscan_item5, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&chanscan_item5, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_txt5, C_ATTR_INACTIVE);
			OSD_SetAttr(&chanscan_sel5, C_ATTR_INACTIVE);
		}
#endif
	}
}

void win_chanscan_set_mode_string(UINT8 input)
{
	switch_scan_mode_select((input==0)? 0:1);
	//add on 2011-10-31
  win_chanscan_scanband_setting();
	OSD_TrackObject((POBJECT_HEAD)&chanscan_item1,C_UPDATE_ALL);
	OSD_DrawObject((POBJECT_HEAD )&chanscan_item2,C_UPDATE_ALL);
	OSD_DrawObject((POBJECT_HEAD )&chanscan_item3,C_UPDATE_ALL);	
	OSD_DrawObject((POBJECT_HEAD )&chanscan_item4,C_UPDATE_ALL);
	OSD_DrawObject((POBJECT_HEAD )&chanscan_item5,C_UPDATE_ALL);
    OSD_DrawObject((POBJECT_HEAD )&chanscan_item6,C_UPDATE_ALL);
}

void init_ch_no_info(Band_param *buf)
{
	UINT16 j=0;
	while(buf[j].start_freq!= 0)
   	{
   		if (j==0)
		{
			if((((buf[j].band_type & 0x0080)==0x0000)&&((buf[j+1].band_type & 0x0080)==0x0080)))
			{
				scan_chan_info.VHF_Start_Ch_No=scan_chan_info.VHF_Current_Ch_No=(buf[j].band_type & 0x7f);
				scan_chan_info.VHF_End_Ch_No=((buf[j].band_type&0x7f00)>>8);		
				scan_chan_info.UHF_Start_Ch_No=scan_chan_info.UHF_Current_Ch_No=(buf[j+1].band_type&0x7f);	
			}
			else if((((buf[j].band_type & 0x0080)==0x0000)&&(buf[j+1].start_freq ==0x0000)))//one VHF
			{
				scan_chan_info.VHF_Start_Ch_No=scan_chan_info.VHF_Current_Ch_No=(buf[j].band_type & 0x7f);
				scan_chan_info.VHF_End_Ch_No=((buf[j].band_type&0x7f00)>>8);						
			}
			else if((((buf[j].band_type & 0x0080)==0x0080)&&(buf[j+1].start_freq ==0x0000)))//one UHF
			{
				scan_chan_info.UHF_Start_Ch_No=scan_chan_info.UHF_Current_Ch_No=(buf[j].band_type & 0x7f);					
				scan_chan_info.UHF_End_Ch_No=((buf[j].band_type&0x7f00)>>8);									
			}
			else if((buf[j].band_type & 0x0080)==0x0080)
				scan_chan_info.UHF_Start_Ch_No=scan_chan_info.UHF_Current_Ch_No=(buf[j].band_type & 0x7f);
			else
				scan_chan_info.VHF_Start_Ch_No=scan_chan_info.VHF_Current_Ch_No=(buf[j].band_type & 0x7f);				
		}
		else if((((buf[j].band_type & 0x0080)==0x0000)&&((buf[j+1].band_type & 0x0080)==0x0080)))
		{
			scan_chan_info.VHF_End_Ch_No=((buf[j].band_type&0x7f00)>>8);
			scan_chan_info.UHF_Start_Ch_No=scan_chan_info.UHF_Current_Ch_No=(buf[j+1].band_type&0x7f);
		}
		else if((((buf[j].band_type & 0x0080)==0x0080)&&(buf[j+1].start_freq ==0x0000)))//set UHF
		{
			scan_chan_info.UHF_End_Ch_No=((buf[j].band_type&0x7f00)>>8);					
		}
		else if((((buf[j].band_type & 0x0080)==0x0000)&&(buf[j+1].start_freq ==0x0000)))//all VHF
		{
			scan_chan_info.VHF_End_Ch_No=((buf[j].band_type&0x7f00)>>8);			
		}
    		j++;

   	}

    scan_chan_info.VHF_Current_Ch_No=scan_chan_info.VHF_Start_Ch_No;
    scan_chan_info.UHF_Current_Ch_No=scan_chan_info.UHF_Start_Ch_No;
}

void channel_param(Band_param *buf,UINT32 chan_no,UINT32 *StartFreq,UINT32 *EndFreq,UINT32 *ChannelNo,UINT32 *Bandwidth,BOOL *band_type)
{          
	UINT16 j=0;
	while(chan_no >((buf[j].band_type&0x7f00)>>8))
	{
		j++;
	}
	*StartFreq=(buf[j].start_freq+(buf[j].bandwidth*(chan_no-(buf[j].band_type&0x007f))));
	*EndFreq=*StartFreq+buf[j].bandwidth;
	*Bandwidth=buf[j].bandwidth;
	*band_type=((buf[j].bandwidth&0x0080)==0x0080?1:0);
	*ChannelNo=chan_no;
}

void get_Band_Type(Band_param *buf,UINT32* frequency,BOOL *BandType)
{
	UINT32 fi=0;
 	while(buf[fi].start_freq!=0)
	{
		if((*frequency<=buf[fi].end_freq)&&(*frequency>=buf[fi].start_freq))
		{
			*BandType=((buf[fi].band_type&0x80)==0x80?1:0);
			break;
		}
		else
		{
			if(fi==0)
			{
				if(*frequency<buf[0].start_freq)
				{
					*BandType=((buf[0].band_type&0x80)==0x80?1:0);	
					break;				
				}
			}
			else if((*frequency>buf[fi-1].end_freq)&&(*frequency<buf[fi].start_freq))
			{     
				*BandType=((buf[fi-1].band_type&0x80)==0x80?1:0);	
				break;
			}
			else
				*BandType=1;
		}    	
		fi++;
	}
}

void win_chanscan_get_setting(UINT32* freq)
{
	EDIT_FIELD	*edf;

	edf = &chanscan_edt4;
	OSD_GetEditFieldIntValue(edf, freq);
}

static VACTION chanscan_item_edt_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
		case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
		case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
		case V_KEY_8:	case V_KEY_9:
			act = key - V_KEY_0 + VACT_NUM_0;
			break;
		case V_KEY_LEFT:
			act = VACT_EDIT_LEFT;
			break;
		case V_KEY_RIGHT:
			act = VACT_EDIT_RIGHT;
			break;		
		default:					
			act = VACT_PASS;
	}

	return act;	 
	 
}

static PRESULT chanscan_item_edt_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 Frequency=0;
	switch(event)
	{
		case EVN_PRE_CHANGE:
			break;
		case EVN_POST_CHANGE:
			win_chanscan_get_setting(&Frequency);
			if(Frequency>(UINT32)((BANDWIDTH_IDX+6)*1000))
			{
				api_stop_timer(&set_frontend);
				set_frontend = api_start_timer("SETANT",500,(OSAL_T_TIMER_FUNC_PTR)chanscan_set_frontend_handler);
				//win_signal_refresh();
			}
			break;
	}
	return ret;
}

static void win_chanscan_set_channel_no(T_NODE *t_node)
{
	UINT32 channel_number=0,channel_number_frequency=0,fi=0;;
	
 	while((t_node->frq !=0)&&(country_param[fi].start_freq!=0))
	{
		if((t_node->frq<=country_param[fi].end_freq)&&(t_node->frq>=country_param[fi].start_freq))
		{
			channel_number_frequency+=(t_node->frq);
			channel_number=(country_param[fi].band_type&0x7F);	
			channel_number+=((channel_number_frequency-country_param[fi].start_freq)/country_param[fi].bandwidth);
			SCAN_BAND_IDX=((country_param[fi].band_type&0x80)!=0?1:0);
			break;
		}
		fi++;
	}
	if(channel_number)
	{
		if(SCAN_BAND_IDX)
			ChNo=scan_chan_info.UHF_Current_Ch_No=channel_number;
		else
			ChNo=scan_chan_info.VHF_Current_Ch_No=channel_number;
	}
}

